package com.yuetong.employee.service.impl;

import java.util.List;
import com.yuetong.common.utils.DateUtils;
import com.yuetong.employee.domain.YtEmployeeEntryInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.yuetong.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.yuetong.employee.domain.YtEmployeeDocuments;
import com.yuetong.employee.mapper.YtEmployeeBasicInfoMapper;
import com.yuetong.employee.domain.YtEmployeeBasicInfo;
import com.yuetong.employee.service.IYtEmployeeBasicInfoService;

/**
 * 职工基本信息Service业务层处理
 * 
 * @author  卓晓君
 * @date 2025-04-24
 */
@Service
public class YtEmployeeBasicInfoServiceImpl implements IYtEmployeeBasicInfoService 
{
    @Autowired
    private YtEmployeeBasicInfoMapper ytEmployeeBasicInfoMapper;

    /**
     * 查询职工基本信息
     * 
     * @param employeeId 职工基本信息主键
     * @return 职工基本信息
     */
    @Override
    public YtEmployeeBasicInfo selectYtEmployeeBasicInfoByEmployeeId(Long employeeId)
    {
        return ytEmployeeBasicInfoMapper.selectYtEmployeeBasicInfoByEmployeeId(employeeId);
    }

    /**
     * 查询职工基本信息列表
     * 
     * @param ytEmployeeBasicInfo 职工基本信息
     * @return 职工基本信息
     */
    @Override
    public List<YtEmployeeBasicInfo> selectYtEmployeeBasicInfoList(YtEmployeeBasicInfo ytEmployeeBasicInfo)
    {
        return ytEmployeeBasicInfoMapper.selectYtEmployeeBasicInfoList(ytEmployeeBasicInfo);
    }

    /**
     * 新增职工基本信息
     * 
     * @param ytEmployeeBasicInfo 职工基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertYtEmployeeBasicInfo(YtEmployeeBasicInfo ytEmployeeBasicInfo)
    {
        ytEmployeeBasicInfo.setCreateTime(DateUtils.getNowDate());
        int rows = ytEmployeeBasicInfoMapper.insertYtEmployeeBasicInfo(ytEmployeeBasicInfo);
        insertYtEmployeeDocuments(ytEmployeeBasicInfo);
        insertYtEmployeeEntryInfo(ytEmployeeBasicInfo);
        return rows;
    }

    private void insertYtEmployeeEntryInfo(YtEmployeeBasicInfo ytEmployeeBasicInfo) {
        List<YtEmployeeEntryInfo> ytEmployeeEntryInfoList = ytEmployeeBasicInfo.getYtEmployeeEntryInfoList();
        if (StringUtils.isNotNull(ytEmployeeEntryInfoList)) {

            List<YtEmployeeEntryInfo> list = new ArrayList<YtEmployeeEntryInfo>();
            for (YtEmployeeEntryInfo ytEmployeeEntryInfo : ytEmployeeEntryInfoList) {
                ytEmployeeEntryInfo.setEmployeeId(ytEmployeeBasicInfo.getEmployeeId());
                list.add(ytEmployeeEntryInfo);
            }
            if (list.size() > 0) {
                ytEmployeeBasicInfoMapper.batchYtEmployeeEntryInfo(list);
            }
        }
    }

    /**
     * 修改职工基本信息
     * 
     * @param ytEmployeeBasicInfo 职工基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateYtEmployeeBasicInfo(YtEmployeeBasicInfo ytEmployeeBasicInfo)
    {
        ytEmployeeBasicInfo.setUpdateTime(DateUtils.getNowDate());
        ytEmployeeBasicInfoMapper.deleteYtEmployeeDocumentsByEmployeeId(ytEmployeeBasicInfo.getEmployeeId());
        insertYtEmployeeDocuments(ytEmployeeBasicInfo);
        ytEmployeeBasicInfoMapper.deleteYtEmployeeEntryInfoByEntryId(ytEmployeeBasicInfo.getEmployeeId());
        insertYtEmployeeEntryInfo(ytEmployeeBasicInfo);
        return ytEmployeeBasicInfoMapper.updateYtEmployeeBasicInfo(ytEmployeeBasicInfo);

    }

    /**
     * 批量删除职工基本信息
     * 
     * @param employeeIds 需要删除的职工基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYtEmployeeBasicInfoByEmployeeIds(Long[] employeeIds)
    {
        ytEmployeeBasicInfoMapper.deleteYtEmployeeDocumentsByEmployeeIds(employeeIds);
        return ytEmployeeBasicInfoMapper.deleteYtEmployeeBasicInfoByEmployeeIds(employeeIds);
    }

    /**
     * 删除职工基本信息信息
     * 
     * @param employeeId 职工基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYtEmployeeBasicInfoByEmployeeId(Long employeeId)
    {
        ytEmployeeBasicInfoMapper.deleteYtEmployeeDocumentsByEmployeeId(employeeId);
        return ytEmployeeBasicInfoMapper.deleteYtEmployeeBasicInfoByEmployeeId(employeeId);
    }
   /**
    * 根据用户id查询职工基本信息
    *
    * @param userId
    * @return
    */
    @Override
    public YtEmployeeBasicInfo selectYtEmployeeBasicInfoByUserId(Long userId) {

        return ytEmployeeBasicInfoMapper.selectYtEmployeeBasicInfoByUserId(userId);
    }

    /**
     * 新增证件照信息信息
     * 
     * @param ytEmployeeBasicInfo 职工基本信息对象
     */
    public void insertYtEmployeeDocuments(YtEmployeeBasicInfo ytEmployeeBasicInfo)
    {
        List<YtEmployeeDocuments> ytEmployeeDocumentsList = ytEmployeeBasicInfo.getYtEmployeeDocumentsList();
        Long employeeId = ytEmployeeBasicInfo.getEmployeeId();
        if (StringUtils.isNotNull(ytEmployeeDocumentsList))
        {
            List<YtEmployeeDocuments> list = new ArrayList<YtEmployeeDocuments>();
            for (YtEmployeeDocuments ytEmployeeDocuments : ytEmployeeDocumentsList)
            {
                ytEmployeeDocuments.setEmployeeId(employeeId);
                list.add(ytEmployeeDocuments);
            }
            if (list.size() > 0)
            {
                ytEmployeeBasicInfoMapper.batchYtEmployeeDocuments(list);
            }
        }
    }
}
